<template>
  <div class="login-container">
    <el-row class="full-height" :gutter="0">
      <!-- 左侧品牌信息 -->
      <el-col :span="12" class="left-panel">
        <div class="brand-wrapper">
          <h1 class="brand-title">家政助手</h1>
          <p class="brand-subtitle">专业 · 可靠 · 让生活更轻松</p>
          <img src="../assets/images/cleaner.png" alt="家政服务" class="brand-image" />
        </div>
      </el-col>

      <!-- 登录表单区域 -->
      <el-col :span="12" class="right-panel">
        <el-card class="login-card" shadow="always">
          <h2 class="login-title">欢迎回来</h2>
          <el-form :model="form" label-position="top" @submit.prevent="handleLogin">
            <el-form-item label="用户名">
              <el-input v-model="form.username" placeholder="请输入用户名" prefix-icon="User" clearable />
            </el-form-item>
            <el-form-item label="密码">
              <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password />
            </el-form-item>
            <el-button type="primary" native-type="submit" class="login-btn" block>登录</el-button>
          </el-form>
          <div class="extra-links">
            <a href="/register">没有账号？立即注册</a>
            <a href="/forget-password">忘记密码？</a>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const router = useRouter()
const form = ref({
  username: '',
  password: ''
})

const handleLogin = async () => {
   if (!form.value.username || !form.value.password) {
    ElMessage.error('用户名和密码不能为空');
    return;
  }

  try {
    const response = await axios.get(`http://localhost:3000/users?username=${form.value.username}`);
    const users = response.data;

    if (users.length > 0 && users[0].password === form.value.password) {
      ElMessage.success('登录成功');
      router.push('/home');
    } else {
      ElMessage.error('用户名或密码错误');
    }
  } catch (error) {
    ElMessage.error('登录失败，请稍后再试');
    console.error('登录时发生错误:', error);
  }
}
</script>

<style scoped>
::v-deep .el-container,
::v-deep .el-container .el-container,
:deep(.el-container),
:deep(.el-container .el-container) {
  display: block !important;
}



.login-container {
  height: 100vh;
  margin: 0;
  padding: 0;
  font-family: 'Segoe UI', sans-serif;
}

.full-height {
  height: 100vh;
  margin: 0;
  padding: 0;
}

.left-panel {
  background: linear-gradient(to bottom right, #4e54c8, #8f94fb);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
  height: 100%;
  box-sizing: border-box;
}

.brand-wrapper {
  text-align: center;
  max-width: 400px;
}

.brand-title {
  font-size: 36px;
  margin-bottom: 10px;
  font-weight: bold;
}

.brand-subtitle {
  font-size: 18px;
  opacity: 0.9;
  margin-bottom: 30px;
}

.brand-image {
  max-width: 100%;
  border-radius: 12px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
}

.right-panel {
  background-color: #f9fafb;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 20px;
  box-sizing: border-box;
}

.login-card {
  width: 420px;
  padding: 40px;
  border-radius: 16px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.1);
  background-color: white;
  transition: all 0.3s ease-in-out;
}

.login-card:hover {
  transform: translateY(-5px);
}
.extra-links {
   display: flex;
  margin-top: 20px;
  text-align: center;
  justify-content: space-between;
  
}

</style>
